<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:OplusCms 後臺項目管理
 *@param datetime 2012-01-06
 *@param author solgh
 ********************/
class Users extends Admins_Controller {

  private $data=array();
  private $admin_id;
  public function __construct(){
    parent::__construct();
    $this->data['currentPage']='systems';
    $this->load->model('admins_mdl');
    $this->data['format_time']=$this->settings['default_time'];
    $this->_get_groups();//獲取組別.
    $lan_get = 	$this->input->get('lan','');
    $lan_get = empty($lan_get) ? get_cookie('language') : $lan_get;
    if(empty($lan_get) || !in_array($lan_get,array('zh-tw','english'))){
      $lan_get = 'english';
    }
    $this->lang->load('page', $lan_get);
    set_cookie('language',$lan_get,time()+24*3600*30);
    $this->data['getlang'] = $lan_get;
    $this->data['lang'] =$this->lang->language;
  }

  public function index(){
    $this->get_list();
    $this->load->view('admins/users_list_view',$this->data);
  }

  //獲取列表
  private function get_list(){
    $page = $this->input->get('p',TRUE);
    $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
    $limit = $this->settings['default_admin_limit'];
    $offset = ($page - 1) * $limit;
    if($offset < 0){
      redirect('admins/users');
    }
    $url_query=$_SERVER['QUERY_STRING'];
    // 保留查詢參數;
    $cookie = array(
        'name'   => 'url_query',
        'value'  => base64_encode($url_query),
        'expire' => '0',
        'path'   => '/',
    );
    $this->input->set_cookie($cookie);
    if(!empty($url_query)){
      $url_query=preg_replace('/&p=(\d+)/','',$url_query);
    }
    $this->data['login_name']=$this->input->get('login_name');
    $this->data['email']=$this->input->get('email');

    $this->data['field']=$this->input->get('field');
    $this->data['sort']=$this->input->get('sort');
    $this->data['field']=!empty($this->data['field'])?$this->data['field']:'n.add_time';
    $this->data['sort']=!empty($this->data['sort'])?$this->data['sort']:'asc';
    $data=array('limits'=>$limit,'offset'=>$offset);

    $data=!empty($this->data['login_name'])?array_merge($data,array('login_name'=>trim($this->data['login_name']))):$data;
    $data=!empty($this->data['email'])?array_merge($data,array('email'=>trim($this->data['email']))):$data;

    $data=!empty($this->data['field'])?array_merge($data,array('field'=>$this->data['field'])):$data;
    $data=!empty($this->data['sort'])?array_merge($data,array('sort'=>strtoupper($this->data['sort']))):$data;

    $this->data['lists_count']=$this->admins_mdl->totle_admins($data);

    $pagination = '';
    if($this->data['lists_count'] > $limit)
    {
      $this->dpagination->currentPage($page);
      $this->dpagination->items($this->data['lists_count']);
      $this->dpagination->limit($limit);
      $this->dpagination->adjacents(5);
      $this->dpagination->target(site_url('admins/users').'?'.$url_query);
      $this->dpagination->parameterName('p');
      $this->dpagination->nextLabel($this->data['lang']['orders_lable_19']);
      $this->dpagination->PrevLabel($this->data['lang']['orders_lable_18']);
      $pagination = $this->dpagination->getOutput();
    }
    $this->data['pagination'] = $pagination;
    $this->data['lists']=$this->admins_mdl->get_admins($data);
  }

  //添加管理員.
  public function add()
  {
    $this->load->view('admins/users_form_view',$this->data);
  }
  //修改信息
  public function edit($edit_id=0)
  {
    if(!empty($edit_id)){
      $result=$this->admins_mdl->get_admins_by_id($edit_id);
      $this->data['edit_id']=$edit_id;
      $this->data['login_name']=$result['login_name'];
      $this->data['group_id']=$result['group_id'];
      $this->data['email']=$result['email'];
      $this->data['sort_order']=$result['sort_order'];
      $this->data['status']=$result['status'];
      $this->load->view('admins/users_form_view',$this->data);
    }else{
      show_error('對不起,參數出錯!');
      exit;
    }
  }
  //執行添加.
  public function modify()
  {
     $edit_id=$this->input->post('edit_id');
     $this->admin_id=$edit_id;
     $this->_load_validation_rules($edit_id);
     if ($this->form_validation->run() == FALSE){
        $this->data['edit_id']=$edit_id;
        $this->data['error']=true;
        $this->load->view('admins/users_form_view',$this->data);
     }else{
        $data=array(
            'login_name'=>$this->input->post('login_name'),
            'group_id'=>$this->input->post('group_id'),
            'email'=>$this->input->post('email'),
            'sort_order'=>$this->input->post('sort_order'),
            'status'=>$this->input->post('status')
        );
        if(!empty($edit_id) && $edit_id>0){
          $operator_title='修改用戶->'.$data['login_name'];
          $action='修改';
          $result=$this->admins_mdl->edit_admins($data,$edit_id);
        }else{
          $operator_title='新增用戶->'.$data['login_name'];
          $action='新增';
          $password=$this->input->post('password');
          $data=array_merge($data,array('login_pwd'=>$password,'add_time'=>time()));
          $result=$this->admins_mdl->add_admins($data);
        }
        $this->operator_log($operator_title,$action,$result);
        $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
        $notify = $result > 0?'success':'error';
        $this->nsession->set_flashdata($notify, $msg);
        $redirect=site_url("admins/users").$this->settings['url_query'];//獲取列表篩選參數
        redirect($redirect);

      }
  }
  //驗證數據格式
  private function _load_validation_rules($edit_id=0){
      $this->form_validation->set_rules('login_name','登入名','trim|required|alpha_dash|is_unique[clickrcms_admin.login_name.id.'.$this->admin_id.']|max_length[20]');
      if(empty($edit_id)){
        $this->form_validation->set_rules('password','登入密碼','trim|required|matches[confirm_pwd]|md5|min_lenght[6]|max_length[100]');
        $this->form_validation->set_rules('confirm_pwd','確認密碼','trim|required|md5|max_length[100]');
      }
      $this->form_validation->set_rules('group_id','組別','trim|required|numeric');
      $this->form_validation->set_rules('email','電郵','trim|valid_email');
      $this->form_validation->set_rules('sort_order','排序','trim|numeric|max_length[3]');
      $this->form_validation->set_rules('status','狀態','trim|required|numeric|max_length[1]');
  }
  //刪除管理員
  public function delete($delete_id=''){
   if(!empty($delete_id)){
      $result=$this->admins_mdl->delete_admins($delete_id);
      $this->operator_log('刪除用戶->ID:'.$delete_id,'刪除',$result);
      $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      $redirect=site_url("admins/users").$this->settings['url_query'];//獲取列表篩選參數
      redirect($redirect);

    }else{
      show_error('對不起,參數出錯');
      exit;
    }
  }

  //修改密碼
  public function changepwd($edit_id=0){
    if(!empty($edit_id)){
      $result=$this->admins_mdl->get_admins_by_id($edit_id);
      $this->data['login_pwd']=$result['login_pwd'];
      $this->data['edit_id']=$edit_id;
      $this->load->view('admins/usres_password_view',$this->data);
    }else{
      show_error('對不起,參數出錯');
      exit;
    }
  }
  //執行修改密碼
  public function modify_changepwd(){
    $this->_load_validation_pwd();
    $login_pwd=$this->input->post('login_pwd');
    $edit_id=$this->input->post('edit_id');
    if ($this->form_validation->run() == FALSE){
        $this->data['error']=true;
        $this->load->view('admins/usres_password_view',$this->data);
     }else{
         $old_pwd=$this->input->post('old_pwd');
         $new_pwd=$this->input->post('new_pwd');
         if($login_pwd!=$old_pwd){
           $this->data['error']=true;
           $this->data['upload_error']=$this->data['lang']['msgerror_lable_3'];
           $this->load->view('admins/usres_password_view',$this->data);
         }else{
           $result=$this->admins_mdl->edit_admins(array('login_pwd'=>$new_pwd),$edit_id);
           $this->operator_log('修改用戶密碼->ID:'.$edit_id,'刪除',$result);
           $msg = $result > 0?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
           $notify = $result > 0?'success':'error';
           $this->nsession->set_flashdata($notify, $msg);
           $redirect=site_url("admins/users").$this->settings['url_query'];//獲取列表篩選參數
           redirect($redirect);

         }
     }
  }
  //驗證執行密碼修改數據格式
  private  function _load_validation_pwd(){
      $this->form_validation->set_rules('old_pwd','原密碼','trim|required|md5|min_lenght[6]|max_length[100]');
      $this->form_validation->set_rules('new_pwd','新密碼','trim|required|matches[confirm_pwd]|md5|min_lenght[6]|max_length[100]');
      $this->form_validation->set_rules('confirm_pwd','確認新密碼','trim|required|md5|max_length[100]');
      $this->form_validation->set_rules('login_pwd','登陸密碼','trim');
      $this->form_validation->set_rules('edit_id','修改id','trim');
  }

  //獲取管理組別
  private function _get_groups(){
    $this->load->model('admins_group_mdl');
    $this->data['groups']=$this->admins_group_mdl->get_admins_group(array('parremns'));
  }
}